package com.globallogic.jira.gadget.renderer;

import java.util.Collection;

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.project.version.Version;

/**
 * 
 * @author anton.pereverziev
 * 
 */
public abstract class AbstractTreeRenderer implements TreeRenderer {

    /**
     * Builds tree template and inserts content and children
     * 
     * @param nodeContent
     * @param insert
     * @return
     */
    String makeTree(String nodeContent, String insert, boolean expansible,
	    int level) {

	StringBuilder result = new StringBuilder();
	result.append("<div class = \"container\" style=\"padding-left: 10px;left: 10px;position:relative;\"> \n");
	result.append("		<div class=\"parentchild\"> \n");
	result.append("		<div class = \"children\" style=\"left: 5px;position:relative; width:80%;\"> \n");
	result.append("			<div class=\"text\"  style=\"left: 5px; position:relative; background: #ffffff; width:100%; padding-top:5px;padding-bottom:5px;\"> \n");// top:10px;
	// adding expanding marker
	if (expansible)
	    result.append("&#1568;");
	else
	    result.append("<a href=\"#\" onclick=\"return UnHide(this)\">&#9660;</a>");
	// Here we adding a node content
	result.append(nodeContent);
	result.append("			</div> \n");
	result.append("		</div> \n");

	// Here we add children
	result.append(insert);
	if (level == 0)
	    result.append("<br/><br/>");
	result.append("	</div> \n");
	result.append("</div> \n");
	return result.toString();
    }

    /**
     * Converts time from seconds to string "x w x d x h x m" or returns
     * "undefined" in case of parameter is null
     * 
     * @param sec
     * @return
     */
    protected String timeConvert(Long sec) {
	String result = "";
	if (sec == null)
	    result = " n/a ";
	else {
	    sec /= 60;
	    result += ((sec % 240000) / 2400) + "w ";
	    result += ((sec % 2400) / 480) + "d ";
	    result += ((sec % 480) / 60) + "h ";
	    result += (sec % 60) + "m ";
	}
	return result;

    }

    /**
     * Gets issue key, converts it in string and strikes it in case issue is
     * resolved
     * 
     * @param issue
     * @return
     */
    protected String getIssueKey(Issue issue) {

	String result = "";
	if (issue.getStatusObject().getName().equals("Resolved")) {
	    result = "<strike>" + issue.getKey() + "</strike>";
	} else
	    result = issue.getKey();
	return result;
    }

    /**
     * Gets issue version, converts it to string or returns "undefined" in case
     * version is not specified
     * 
     * @param issue
     * @return
     */
    protected String getVersions(Issue issue) {
	String result = "";
	Collection<Version> versions = issue.getFixVersions();
	if (versions == null || versions.size() == 0)
	    result = "n/a";
	else {
	    for (Version version : versions) {
		result += version.getName() + " ";
	    }
	}

	return result;
    }

    public String getHTML(int level, Issue issue, String insert,
	    boolean expansible) {

	if (level > 10)
	    return "<br/>Nesting level is > 10. Further issues are not shown.";

	return makeTree(getNodeContent(issue), insert, expansible, level);
    }

    abstract protected String getNodeContent(Issue issue);

    public String getHTMLHead(String projectName, String issueType,
	    String fixVersion, String linkType, String includeSubtasks) {
	return "<div style=\"text-align: center\"><span style=\"font-size: 18pt;\">Project: </span><span style=\"font-size: 20pt;color: blue;\">"
		+ projectName + "</span></span></div>";
    }

}
